package com.qujia.station.controller;

import cn.hutool.core.lang.Validator;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.exception.CustomException;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.SysAppUserInfo;
import com.qujia.station.domain.dto.SyncUserDto;
import com.qujia.station.service.IAppUserInfoService;
import com.qujia.station.service.IStationService;
import com.qujia.station.service.UserSyncService;
import com.qujia.station.utils.SplitListUtils;
import com.qujia.system.domain.SysAppUserInfoUser;
import com.qujia.system.service.IAppUserInfoUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

@RestController
@RequestMapping("/syncUser")
@Slf4j
public class UserSyncController {

    @Resource
    private UserSyncService userSyncService;

    @PostMapping("/addUser/{timestamp}/{sign}")
    public AjaxResult syncUser(@RequestBody @Valid SyncUserDto syncUserDto){
        if(!Validator.isMobile(syncUserDto.getMobilePhone())){
            throw new CustomException("手机号格式错误");
        }
        userSyncService.insertUser(syncUserDto);
        return AjaxResult.success("新增用户成功");
    }

    @PostMapping("/batchSyncUser/{timestamp}/{sign}")
    public AjaxResult batchSyncUser(@RequestBody  List<SyncUserDto> syncUserDtoList){
        log.info("开始批量存入云化用户【{}】",syncUserDtoList);

        List<List<SyncUserDto>> lists = SplitListUtils.splitList(20, syncUserDtoList.size(), syncUserDtoList);
        CountDownLatch countDownLatch = new CountDownLatch(lists.size());
        for (List<SyncUserDto> itemList : lists) {
            userSyncService.batchInsertUser(itemList,countDownLatch);
        }



        return AjaxResult.success("批量新增用户成功");
    }
}
